package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Admin;
import com.hcrazy.manchester.app.pojo.PageModel;

public class AdminDAO extends BaseDAO<Admin>{

	public Admin getAdminByUsernameOrEmail(Admin admin) throws Exception{
		String sql = "select * from him_admin where adminName = ? or adminEmail = ?";
		return this.getJavaBean(sql, Admin.class, admin.getAdminName(),admin.getAdminEmail());
	}
	
	public Admin getAdminByName(String adminName) throws Exception{
		String sql = "select * from him_admin where adminName = ?";
		return this.getJavaBean(sql, Admin.class,adminName);
	}
	
	public Admin getAdminByEmail(String email) throws Exception{
		String sql = "select * from him_admin where adminEmail = ?";
		return this.getJavaBean(sql, Admin.class,email);
	}
	
	public void addAdmin(Admin admin) throws Exception{
		String sql = "insert into him_admin (";
		sql += "adminName,adminPassword,adminLevel,adminEmail,registerTime,adminPhone";
		sql += ") values (";
		sql += ":adminName,:adminPassword,:adminLevel,:adminEmail,:registerTime,:adminPhone";
		sql += ")";
		
		this.update(sql, admin);
	}
	
	public void updateAdmin(Admin admin) throws Exception{
		String sql = "update him_admin set ";
		sql += "adminName = :adminName,adminPassword=:adminPassword,adminEmail=:adminEmail,adminPhone=:adminPhone";
		sql += " where adminId = :adminId";
		
		this.update(sql, admin);
	}
	
	public Admin getAdmin(int adminId) throws Exception{
		String sql = "select * from him_admin where adminId = ?";
		return this.getJavaBean(sql, Admin.class,adminId);
	}
	
	public void deleteAdmin(int adminId) throws Exception{
		String sql = "delete from him_admin where adminId = ?";
		this.commonUpdate(sql, adminId);
	}
	
	public PageModel getPagedAdmins(PageModel model) throws Exception{
		StringBuilder querySql = new StringBuilder("select * from him_admin ");
		StringBuilder countSql = new StringBuilder("select count(*) from him_admin");
		StringBuilder whereSql = new StringBuilder(" where adminLevel = ?");
		List<Object> paramList = new ArrayList<Object>();
		
		return this.getPageModel(model, querySql, countSql, whereSql, paramList, Admin.class);
	}
}
